import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import {FORM_OPTIONS} from '../constants';
import {PERMISSION, currencySattus} from './constants';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import Export from '../common/Export';
import {injectIntl, language} from './intl';
import WrappedDatePicker from '../common/WrappedDatePicker';
import WrappedSelect from '../common/WrappedSelect';
const FormItem = Form.Item;

const currencySattusList = currencySattus.toList(language);
class QueryPanel extends React.PureComponent {
    componentDidMount() {
        if(!this.props.isDealerUser)
            this.props.onConditionsChange('dealerId', this.props.enterprise.id);
    }
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }

    onSelectDealer = data => {
        this.props.onConditionsChange('dealerId', data.id);
        this.props.onConditionsChange('currency', data.currency);
        this.props.onQueryDealerChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }
    
    render() {
        const dealerInfo = this.props.conditions.dealerId ? {
            id: this.props.queryDealer.id,
            code: this.props.queryDealer.code,
            name: this.props.queryDealer.name
        } : null;
        return (
            <Form className="form-standard">
                <Row>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.dealerId',
                                defaultMessage: '总代编号'
                            })}
                            {...FORM_OPTIONS.item}>
                            {this.props.isDealerUser
                                ? (<span>{`(${this.props.enterprise.code})${this.props.enterprise.name}`}</span>)
                                : (<DealerSelectPanel
                                    value={dealerInfo}
                                    onSelect={this.onSelectDealer} />)
                            }
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.currency',
                                defaultMessage: '币种'
                            })}
                            {...FORM_OPTIONS.item}>
                            <WrappedSelect
                                name="currency"
                                allowClear
                                value={this.props.conditions.currency}
                                options={currencySattusList}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.startDate',
                                defaultMessage: '开始日期'
                            })}
                            {...FORM_OPTIONS.item}>
                            <WrappedDatePicker
                                name="startDate"
                                value={this.props.conditions.startDate}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.endDate',
                                defaultMessage: '结束日期'
                            })}
                            {...FORM_OPTIONS.item}>
                            <WrappedDatePicker
                                name="endDate"
                                value={this.props.conditions.endDate}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                </Row>
                <Row className="operation-buttons">
                    <Col span={6}>
                        <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>
                            {this.props.intl.formatMessage({
                                id: 'queryPanel.btn.query',
                                defaultMessage: '查询'
                            })}
                        </Button>
                        <Button key="clear" onClick={this.props.onResetClick}>
                            {this.props.intl.formatMessage({
                                id: 'queryPanel.btn.reset',
                                defaultMessage: '重置'
                            })}
                        </Button>
                    </Col>
                    <Col span={18} className="col-align-right">
                        <Export key={PERMISSION.export} exportRequest={this.props.exportData} />
                    </Col>
                </Row>
               
            </Form>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onQueryDealerChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    exportData: PropTypes.func,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    queryDealer: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    saveQueryCondition,
    resetQueryPanel,
    querySearch,
    saveQueryDealer,
    exportData
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealer']);
const getInitEnterprise = selectorFactory(['page', 'domainData', 'initData', 'enterprise']);
const mapStateToProps = state => ({
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
    enterprise: getInitEnterprise(state),
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser'])),
    conditions: getConditions(state),
    queryDealer: getQueryDealer(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    onQueryDealerChange: data => dispatch(saveQueryDealer(data)),
    querySearch: () => dispatch(querySearch()),
    exportData: () => dispatch(exportData()),
    onResetClick: () => dispatch(resetQueryPanel()),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
